<?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 * User Plugin
 *
 * Run checks on a users status
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Plugins
 */
class Plugin_User extends Plugin
{

	
	public $user_profile_data = array();

	public function logged_in()
	{
		$group = $this->attribute('group', null);

		if ($this->current_user)
		{
			if ($group and $group !== $this->current_user->group)
			{
				return '';
			}

			return $this->content() ? $this->content() : true;
		}

		return '';
	}

	/**
	 * Not logged in
	 *
	 * See if a user is logged out or not part of a group
	 *
	 * Usage:
	 *
	 *     {{ user:not_logged_in group="admin" }}
	 *            <p>Hello not an admin</p>
	 *     {{ endif }}
	 *
	 * @return boolean State indicator.
	 */
	public function not_logged_in()
	{
		$group = $this->attribute('group', null);

		// Logged out or not the right user
		if (!$this->current_user or ($group and $group !== $this->current_user->group))
		{
			return $this->content() ? $this->content() : true;
		}

		return '';
	}

	/**
	 * Has Control Panel permissions
	 *
	 * See if a user can access the control panel.
	 *
	 * Usage:
	 *
	 *     {{ user:has_cp_permissions}}
	 *         <a href="/admin">Access the Control Panel</a>
	 *     {{ endif }}
	 *
	 * @return boolean State indicator.
	 */
	public function has_cp_permissions()
	{
		if ($this->current_user)
		{
			if (!(($this->current_user->group == 'admin') or $this->permission_m->get_group($this->current_user->group_id)))
			{
				return '';
			}

			return $this->content() ? $this->content() : true;
		}

		return '';
	}

	
	public function __call($name, $data)
	{
		if (in_array($name, array('password', 'salt')))
		{
			return;
		}

		$user_id = $this->attribute('user_id', null);

		// If we do not have a user id and there is
		// no currently logged in user, there's nothing we can do
		if (is_null($user_id) and !isset($this->current_user->id))
		{
			return null;
		}
		elseif (is_null($user_id) and isset($this->current_user->id))
		{
			// Otherwise, we can use the current user id
			$user_id = $this->current_user->id;

			// but first, is it data we already have? (such as user:username)
			if (isset($this->current_user->{$name}))
			{
				return $this->current_user->{$name};
			}
		}
		
		// we're fetching a different user than the currently logged in one
		return $this->get_user_var($name, $user_id);
	}
	
	

}